# auth.py
from flask import Blueprint, jsonify, request
from flask_jwt_extended import (get_jwt_identity, jwt_required)
import flaskapp.services.media as service
import logging

logger = logging.getLogger(__name__)
media_bp = Blueprint('mediasource', __name__)

@media_bp.route('/api/mediasource', methods=['POST'])
@jwt_required()
def createSource():
    try:
        current_user = get_jwt_identity()
        print(current_user)
        name = request.json.get("name", None)
        url = request.json.get("url", None)
        code = request.json.get("code", "unkown")
        width = int(request.json.get("width", "0",))
        height = int(request.json.get("height", "0"))
   
        ret = service.insert(name=name, url=url, code=code, width=width,height=height)

        response = jsonify({"code": 0, "message": ret})
        return response
    except Exception as e:
        logger.error(f"Error getting data: {e}")
        return jsonify({"code": -1, "message": e.args})

@media_bp.route('/api/mediasource', methods=['PUT'])
@jwt_required()
def updateSource():
    try:
        current_user = get_jwt_identity()
        print(request.json)
        id = request.json.get("id", None)
        name = request.json.get("name", None)
        url = request.json.get("url", None)
        code = request.json.get("code", "unkown")
        width = int(request.json.get("width", "0"))
        height = int(request.json.get("height", "0"))
        ret=service.update(id,name=name, url=url, code=code, width=width,height=height)
        response = jsonify({"code": 0, "message": ret})
        return response
    except Exception as e:
        logger.error(f"Error getting data: {e.args}")
        return jsonify({"code": -1, "message": e.args})

@media_bp.route('/api/mediasource', methods=['GET'])
@jwt_required()
def list_source():
    try:
        current_user = get_jwt_identity()
        print(current_user)
        pageIndex = int(request.values.get('pageIndex',1))
        pageSize = int(request.values.get('pageSize',10))
        ret = service.find(pageIndex,pageSize)
        response = jsonify({"code": 0, "message": "success", "data": ret})
        return response
    except Exception as e:
        logger.error(f"Error getting data: {e}")
        return jsonify({"code": -1, "message": e.args})


@media_bp.route('/api/mediasource/<id>', methods=["DELETE"])
@jwt_required()
def deleteSource(id):
    try:
        current_user = get_jwt_identity()
        ret = service.delete(id)
        return jsonify({"code": -1, "message": "success", "data": ret})
    except Exception as e:
        logger.error(f"Error getting data: {e}")
        return jsonify({"code": -1, "message": e.args})

@media_bp.route('/api/mediasource/url', methods=['GET'])
@jwt_required()
def get_media_url():
    try:
        data = service.get_media_url()
        return jsonify({"code": 0, "data": data})
    except Exception as e:
        logger.error(f"Error getting mediasource items: {e}")
        return jsonify({"error": "Failed to get mediasource items"})